/**
 *@author   wuwg
 *@createTime   2018/03/20
 *@updateTime   2018/03/21
 *@description  module  component
 */
import config from '../../../config/config.js'

// import Options from './options.vue';
import Locale from '../../../mixins/locale.js'
import atySelect from '../../form/select/index.vue'
// 前缀
const prefixCls = config.prefix + '-pagination'
//
function isValueNumber (value) {
  return (/^[1-9][0-9]*$/).test(value + '')
}
// module
const module = {
  name: config.prefix + '-pagination-options',
  mixins: [Locale],
  components: { atySelect },
  props: {
    pageSizeOpts: Array,
    showSizer: Boolean,
    showElevator: Boolean,
    current: Number,
    _current: Number,
    pageSize: Number,
    allPages: Number,
    isSmall: Boolean,
    placement: String,
    transfer: Boolean
  },

  data () {
    return {
      currentPageSize: this.pageSize,
      scope: {
        className: config.prefix + '-pagination-options',
        pageInputWidth: 50
      }
    }
  },
  watch: {
    pageSize (val) {
      this.currentPageSize = val
    },
    _current (val) {
      if (val > 9999) {
        this.scope.pageInputWidth = (val.toString().length - 4) * 5 + 50
      } else {
        this.scope.pageInputWidth = 50
      }
    }
  },
  computed: {
    currentPageSizeOpts () {
      var pageSizeOptsArr = this.pageSizeOpts.map((item, index) => {
        return {
          code: item,
          // codeType:'shenzhen',
          name: item + (this.t('i.page.page'))
        }
      })
      return pageSizeOptsArr
    },
    size () {
      return this.isSmall ? 'small' : 'default'
    },
    optsClasses () {
      return [
                `${prefixCls}-options`
      ]
    },
    sizerClasses () {
      return [
                `${prefixCls}-options-sizer`
      ]
    },
    ElevatorClasses () {
      return [
                `${prefixCls}-options-elevator`
      ]
    }
  },
  methods: {
    changeSize () {
      this.$emit('on-size', this.currentPageSize)
    },
    changePage (event) {
      let val = event.target.value.trim()
      let page = 0

      if (isValueNumber(val)) {
        val = Number(val)
        if (val != this.current) {
          const allPages = this.allPages

          if (val > allPages) {
            page = allPages
          } else {
            page = val
          }
        }
      } else {
        page = 1
      }

      if (page) {
        this.$emit('on-page', page)
        event.target.value = page
      }
    }
  },
  updated () {

  },
  created () {

  },
  mounted () {

  }
}
export default module
